//
//  NewsDetailViewController.m
//  555彩票
//
//  Created by zhoubo on 2017/7/4.
//  Copyright © 2017年 zhoubo. All rights reserved.
//

#import "NewsDetailViewController.h"
#import <UIKit/UIKit.h>

#import "BmobSDK/Bmob.h"
#import "SVProgressHUD.h"

@interface NewsDetailViewController ()<UIWebViewDelegate>

@property (nonatomic,strong) NSString* date;

@property (nonatomic,copy) NSString* content;
@property (nonatomic,copy) NSString* author;
@property (nonatomic,strong) NSArray* imgarr;

@property (nonatomic,copy) NSString* ctitle;

@property (nonatomic,weak) UIWebView* webview;

@property (nonatomic,strong) UIScrollView* scrollview;

@property (nonatomic,strong) UILabel* contentlbl;



@end

@implementation NewsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self loadwithUIWwebview];
    

    
    
    
    
    
    
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = false;
}

-(void)loadwithUIWwebview
{
    UIWebView* webview = [[UIWebView alloc]initWithFrame:self.view.bounds];
    [self.view   addSubview:webview];
    self.webview = webview;
    
    self.webview.delegate = self;
    
//    self.webview.backgroundColor = [UIColor w];
    
    
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"detail.html" withExtension:nil];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webview loadRequest:request];
    [SVProgressHUD showWithStatus:@"加载中..."];
    
    
    
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
}



- (void)webViewDidFinishLoad:(UIWebView *)webView {


    NSString *js = [NSString stringWithFormat:@"changeContent('%@','%@','%@');",self.ctitle,self.date,self.content];
    [self.webview stringByEvaluatingJavaScriptFromString:js];
    [SVProgressHUD dismiss];
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}




-(void)setNews:(News *)news
{
    _news = news;
    
    // 如果模型的id大于1000 简单label加载
    if ([news.Id integerValue] > 1000) {
        
        NSLog(@"title is %@ more than 1000",self.news.title);
        [self.webview removeFromSuperview];
        self.scrollview = [[UIScrollView alloc]initWithFrame:self.view.bounds];
        [self.view addSubview: self.scrollview];
        self.contentlbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height)];
        [self.scrollview addSubview:self.contentlbl];
        self.contentlbl.textAlignment = NSTextAlignmentLeft;
        self.contentlbl.text = self.news.content;
        self.contentlbl.numberOfLines = 0;
        [self.contentlbl sizeToFit];
        self.scrollview.contentSize = CGSizeMake(0, self.contentlbl.bounds.size.height+64);
    }
    
    // 获得新闻时间和来源
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    self.date = [dateFormatter stringFromDate:[NSDate date]];

    self.author = self.news.author;
    self.ctitle = self.news.title;
    self.content = self.news.content;
    self.imgarr = self.news.imgurls;
    
    
    self.content = [self.content stringByAppendingString:@"</p>"];
    self.content = [NSString stringWithFormat:@"<p>%@",self.content];
    

    self.content  = [self.content stringByReplacingOccurrencesOfString:@"\\" withString:@"®"];
    self.content = [self.content stringByReplacingOccurrencesOfString:@"®n" withString:@"</p><p>"];
    

//    self.content = [self.content stringByReplacingOccurrencesOfString:@"</p><p>" withString:[NSString stringWithFormat:@"</p>%@<p>",imgTag]];
    
    
    NSArray* arr = [self.content componentsSeparatedByString:@"</p><p>"];
    
    NSMutableArray* marr = [NSMutableArray arrayWithArray:arr];
    
    for (int i = 0; i<marr.count; i++)
    {
        if (i < self.imgarr.count) {
            
            NSString *imgTag = [NSString stringWithFormat:@"<img src=\"%@\" width=\"100%%\"/>",self.imgarr[i]];
            marr[i] = [marr[i] stringByAppendingString:[NSString stringWithFormat:@"</p>%@<p>",imgTag]];
            
            
        }
        else
        {
            marr[i] =[marr[i] stringByAppendingString:@"</p><p>"];
        }
    }
    
    self.content = [marr componentsJoinedByString:@""];


    
    
    
}

@end
